package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//跨域请求的一种方式 不行
// @CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    //新增员工
    @PostMapping
    public CommonResult save(@RequestBody Category category){
        // System.out.println(category);
        boolean addcategory = categoryService.save(category);
        return CommonResult.success(addcategory);
    }
    @GetMapping("list")
    public CommonResult getList(Integer type){
        List<Category> list = categoryService.getList(type);
        return CommonResult.success(list);
    }


    //@RequestParam("page") 默认是有的  请求参数  defaultValue = "1"  默认值是1
    @GetMapping("/page")
    public CommonResult page(
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Category> pageInfo = categoryService.page( page, pageSize);
        return CommonResult.success(pageInfo);
    }
    //修改员工
   @PutMapping
    public CommonResult update(@RequestBody Category category){
       boolean upd = categoryService.update(category);
       return CommonResult.success("修改分类信息成功");
   }
   @DeleteMapping
    public CommonResult delete( String id){
       boolean delete = categoryService.delete(id);
       return CommonResult.success("删除成功");
   }
}
